package com.ityu.controller;

import com.ityu.exception.InsertSameException;
import com.ityu.pojo.Student;
import com.ityu.pojo.pagePojo.StudentPage;
import com.ityu.pojo.result.Result;
import com.ityu.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping()
    public Result pageFindStudents(StudentPage studentPage) {
        log.info("-----StudentController-----分页查询学生！参数为{}", studentPage);
        return Result.success(studentService.pageFindStudents(studentPage));
    }

    @DeleteMapping("/{ids}")
    public Result deleteStudents(@PathVariable List<Integer> ids) {
        log.info("-----StudentController-----删除学生！参数为{}", ids);
        studentService.deleteStudents(ids);
        return Result.success();
    }

    @PostMapping()
    public Result addStudent(@RequestBody Student student) {
        log.info("-----StudentController-----新增学生！参数为{}", student);
        try {
            studentService.addStudent(student);
        } catch (InsertSameException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("添加学生失败");
        }
        return Result.success();
    }


    @GetMapping("/{id}")
    public Result selectStudentById(@PathVariable Integer id) {
        log.info("-----StudentController-----根据Id查询学生！参数为{}", id);
        return Result.success(studentService.selectStudentById(id));
    }


    @PutMapping()
    public Result updateStudent(@RequestBody Student student) {
        log.info("-----StudentController-----修改学生！参数为{}", student);
        studentService.updateStudent(student);
        return Result.success();
    }

    @PutMapping("/violation/{id}/{score}")
    public Result addViolation(@PathVariable Integer id, @PathVariable Integer score) {
        log.info("-----StudentController-----添加学生违纪！参数为{}", id);
        studentService.addViolation(id, score);
        return Result.success();
    }


}
